<template>
	<view class="uni-container">
		<view :class="{'pc-hide': hideList.indexOf(item.id) !== -1  && hasLeftWin}" class="uni-panel"
			v-for="item in lists" :key="item.id">
			<view :class="{'left-win-active': isClicked ===  item.id && hasLeftWin}" class="uni-panel-h"
				@click="goDetailPage(item.id)">
				<text class="uni-panel-text">{{item.name}}</text>
				<text class="uni-panel-icon uni-icon">&#xe470;&#x2026;</text>
			</view>
		</view>
	</view>
</template>

<script>
	import http from '../../common/request.js'

	export default {

		props: {
			hasLeftWin: {
				type: Boolean
			},
			leftWinActive: {
				type: String
			}
		},
		data() {
			return {
				hideList: [
					'load-more'
				],
				lists: [],
				lists2: [{
					name: "仓库信息",
					url: "goods_card"
				}],
				hasRedirected: false, // 标志位防止重复跳转
				isClicked: ''
			}
		},
		onLoad() {
			if (!this.hasLeftWin) {
				// 检查登录状态或其他条件
				if (!this.hasRedirected && !this.isLoggedIn()) {
					this.hasRedirected = true;
					//window.location.href = '/#/pages/login/login';
					return;
				}
			}

		},
		onShow() {
			console.log('show:', this.lists);
		},
		mounted() {
			this.getNoteBook(true);
			//this.isClicked =  this.lists[0].id;
			console.log(this.lists[0]);
			console.log('mounted:', this.lists);
		},
		onReady() {
			// #ifdef APP-NVUE
			console.log('onReady')

			// #endif
		},
		onShareAppMessage() {
			return {
				title: '欢迎体验first/first',
				path: '/pages/first/first'
			}
		},
		onNavigationBarButtonTap(e) {
			uni.navigateTo({
				url: '/pages/about/about'
			});
		},

		watch: {
			// 监听菜单是否由更新
			'$globalData.menuAdd': {
				handler(newVal, oldVal) {
					console.log('主题变化:', newVal, oldVal);
					this.getNoteBook(false);
				},
				immediate: false // 初始化时立即触发一次
			}
		},

		methods: {
			goDetailPage(notebookId) {
				const url = '/pages/home/home?notebookId=' + notebookId;
				if (this.hasLeftWin) {
					uni.reLaunch({
						url: url
					})
				} else {
					uni.navigateTo({
						url: url
					})
				}
				this.isClicked = notebookId;
				this.$globalData.currentNotebookId = notebookId;
			},
			isLoggedIn() {
				return uni.getStorageSync('token') !== '';
			},
			getNoteBook(ifReClicked) {
				// ifReClicked: 该参数用于表示是否需要刷新menu页面
				http.requestWithToken({
						url: '/notebook',
						method: 'GET'
					}).then(({
						data
					}) => {
						// 清空表格数据
						this.lists = [];

						// 直接映射数据结构，避免forEach+push
						this.lists = data.map(item => ({
							id: item.id,
							name: item.notebook,
						}));
						console.log('getNoteBook', this.lists);
						if (ifReClicked){
							this.isClicked = this.lists[0].id;
						}
					})
					.catch(err => {
						console.error('请求出错:', err);
						uni.showToast({
							title: "请求出错：" + err,
							icon: "none",
							duration: 2000
						});
						// 可以在这里添加错误提示逻辑
					});
			}
		}
	}
</script>

<style>
	@import '../../common/uni-nvue.css';
</style>